package groep3.ssc.main;

import groep3.ssc.coachViews.NewRaceManager;
import groep3.ssc.coachViews.SchemeManager;
import groep3.ssc.coachViews.SkateViewManagerActivity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

@SuppressWarnings("deprecation")
public class CoachFragment extends Fragment implements OnTabChangeListener
{
    private TabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.coach_view, container, false);

        mTabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
        mTabHost.setOnTabChangedListener(this);
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
        mLocalActivityManager.dispatchCreate(savedInstanceState); // state will be bundle your activity state which you get in onCreate
        mTabHost.setup(mLocalActivityManager);

        setUpTabHost();
        return rootView;
    }

    private void setUpTabHost()
    {
        Resources res = getResources();
        TabHost.TabSpec tabSpec;
        Intent intent;

        String schemeManagerString = res.getString(R.string.txtSchemeManagerString);
        String skaterViewManagerString = res.getString(R.string.txtSkaterViewManagerString);
        String startANewRaceString = res.getString(R.string.txtStartANewRaceString);
        
        intent = new Intent().setClass(getActivity(), SchemeManager.class);
        tabSpec = mTabHost.newTabSpec(schemeManagerString).setIndicator(schemeManagerString, res.getDrawable(R.drawable.ic_launcher))
                .setContent(intent);
        mTabHost.addTab(tabSpec);

        intent = new Intent().setClass(getActivity(), SkateViewManagerActivity.class);
        tabSpec = mTabHost.newTabSpec(skaterViewManagerString)
                .setIndicator(skaterViewManagerString, res.getDrawable(R.drawable.ic_launcher)).setContent(intent);
        mTabHost.addTab(tabSpec);

        intent = new Intent().setClass(getActivity(), NewRaceManager.class);
        tabSpec = mTabHost.newTabSpec(startANewRaceString).setIndicator(startANewRaceString, res.getDrawable(R.drawable.ic_launcher))
                .setContent(intent);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId)
    {
        final InputMethodManager imm = (InputMethodManager) Main.viewPager.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(Main.viewPager.getWindowToken(), 0);//hide keyboard
    }
}